package com.weibo.survey.controller;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.weibo.survey.domain.Page;
import com.weibo.survey.domain.Question;
import com.weibo.survey.service.AnswerService;
import com.weibo.survey.service.QuestionService;
import com.weibo.survey.service.impl.AnswerServiceImpl;

@Controller
@Scope("prototype")
public class QuestionAction extends BaseAction<Question> {

	private static final long serialVersionUID = 4445321617682136754L;
	private int pid;
	private int sid;
	@Resource(name = "questionService")
	private QuestionService questionService;
	
	@Resource(name = "answerService")
	private AnswerService answerService;

	public String toSelectQtPage() {
		return "toSelectQtPage";
	}

	public String toDesignQuestionPage() {
		return String.valueOf(model.getQuestionType());
	}

	public String saveOrUpdateQuestion() {
		model.setPage(new Page(pid));
		questionService.saveOrUpdateEnity(model);
		return "designSurveyAction";
	}

	public String editQuestion() {
		this.model = questionService.getEntity(model.getId());
		this.model.setPage(new Page(pid));
		return String.valueOf(model.getQuestionType());
	}

	public String deleteQuestion() {
		// 1.删除问题的答案
		String hql = "delete from Answer a where a.questionId=?";
		answerService.batchByHql(hql, model.getId());
		// 2.删除问题
		hql = "delete from Question q where q.id=?";
		questionService.batchByHql(hql, model.getId());
		return "designSurveyAction";
	}

	public int getPid() {
		return pid;
	}

	public void setPid(int pid) {
		this.pid = pid;
	}

	public int getSid() {
		return sid;
	}

	public void setSid(int sid) {
		this.sid = sid;
	}

}
